module(
    name = "rules_zstd",
    version = "1.0.0-beta.6",
    bazel_compatibility = [
        ">=7.1.0",
    ],
    compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.0")
bazel_dep(name = "toolchain_utils", version = "1.0.0-beta.17")
bazel_dep(name = "ape", version = "1.0.0-beta.14")
bazel_dep(name = "zstd", version = "1.5.6", repo_name = "zstandard")
bazel_dep(name = "download_utils", version = "1.0.0-beta.4")

bazel_dep(name = "hermetic_cc_toolchain", version = "3.1.0", dev_dependency = True)

download_archive = use_repo_rule("@download_utils//download/archive:defs.bzl", "download_archive")

download_archive(
    name = "superconfigure",
    srcs = ["bin/zstd"],
    integrity = "sha256-M2r7xysRZysvprIT1Gpuaygy34B1fws+OBvMsp3vRbA=",
    urls = ["https://github.com/ahgamut/superconfigure/releases/download/z0.0.55/compress.zip"],
)

export = use_extension("@toolchain_utils//toolchain/export:defs.bzl", "toolchain_export")
use_repo(export, "ape-zstd")
export.symlink(
    name = "zstd",
    target = "@ape-zstd",
)
use_repo(export, "zstd")

resolved = use_repo_rule("@toolchain_utils//toolchain/resolved:defs.bzl", "toolchain_resolved")

resolved(
    name = "resolved-zstd",
    toolchain_type = "//zstd/toolchain/zstd:type",
)

# Higher preference to `ape` toolchain
# https://github.com/ahgamut/superconfigure/issues/51
register_toolchains(
    "//zstd/toolchain/zstd:superconfigure-amd64-freebsd",
    "//zstd/toolchain/zstd:superconfigure-amd64-linux",
    "//zstd/toolchain/zstd:superconfigure-amd64-macos",
    "//zstd/toolchain/zstd:superconfigure-amd64-netbsd",
    "//zstd/toolchain/zstd:superconfigure-amd64-openbsd",
    "//zstd/toolchain/zstd:superconfigure-amd64-windows",
    "//zstd/toolchain/zstd:superconfigure-arm64-freebsd",
    "//zstd/toolchain/zstd:superconfigure-arm64-linux",
    "//zstd/toolchain/zstd:superconfigure-arm64-macos",
)

register_toolchains("//zstd/toolchain/...")
